package kudu;


import org.apache.kudu.ColumnSchema;
import org.apache.kudu.Schema;
import org.apache.kudu.Type;
import org.apache.kudu.client.CreateTableOptions;
import org.apache.kudu.client.KuduClient;
import org.apache.kudu.client.KuduException;

import java.util.ArrayList;

public class createTable {
    public static void main(String[] args)  {
        //必须指定表连接到的master节点主机名
        String masterAddr="linux122";
        KuduClient.KuduClientBuilder kuduClientBuilder = new KuduClient.KuduClientBuilder(masterAddr);
        KuduClient client = kuduClientBuilder.build();


        //必须定义schema,声明的每一个字段必须显式的说明是否是主键
        ArrayList<ColumnSchema> columnSchemas = new ArrayList<>();
        ColumnSchema id = new ColumnSchema.ColumnSchemaBuilder("id", Type.INT32).key(true).build();
        ColumnSchema name = new ColumnSchema.ColumnSchemaBuilder("name", Type.STRING).key(false).build();

        columnSchemas.add(id);
        columnSchemas.add(name);


        //必须指定副本数量、分区策略和数量
        CreateTableOptions option = new CreateTableOptions();
        //设定当前副本数量为1
        option.setNumReplicas(1);
        //设定分区信息
        ArrayList<String> colrule = new ArrayList<>();
        colrule.add("id");
        option.addHashPartitions(colrule,3);
        try {
            client.createTable("student",new Schema(columnSchemas),option);
        } catch (KuduException e) {
            e.printStackTrace();
        } finally {
            try {
                client.close();
            } catch (KuduException e) {
                e.printStackTrace();
            }
        }

    }

}
